import java.util.PriorityQueue;

public class KthLargestElement {
    private PriorityQueue<Integer> minHeap;
    private int k;
    public KthLargestElement(int k,int[] nums){
        this.k = k;
        this.minHeap = new PriorityQueue<>();
        for(int num : nums){
            add(num);
        }
    }
    public int add(int val){
        if(minHeap.size() < k){
            minHeap.add(val);
        }else if(val > minHeap.peek()){
            minHeap.poll();
            minHeap.offer(val);
        }
        return minHeap.peek();
    }
}
